package com.irdstudio.efp.esb.service.impl.dxm;

import java.util.Objects;

import com.alibaba.fastjson.JSONObject;
import com.irdstudio.efp.esb.common.constant.dxm.DxmEnum;
import com.irdstudio.efp.esb.common.constant.dxm.reqbuilder.DXMBasicInfo;
import com.irdstudio.efp.esb.common.constant.dxm.reqbuilder.DXMReqBodyBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;


import com.irdstudio.basic.framework.core.exception.ESBException;
import com.irdstudio.efp.esb.common.constant.dxm.DxmConstant;
import com.irdstudio.efp.esb.service.bo.req.dxm.ReqGetImageFileBean;
import com.irdstudio.efp.esb.service.bo.resp.dxm.RespGetImageFileBean;
import com.irdstudio.efp.esb.service.client.ESBClientFactory;
import com.irdstudio.efp.esb.service.client.impl.LhxESBBeanCreator;
import com.irdstudio.efp.esb.service.facade.dxm.GetImageFileService;

/**
 * 获取影像接口实现类: <br>
 *
 * 0.1:caoxin:2019/3/25 10:33: 获取影像接口实现类: <br>
 * 0.2:caoxin:2019/3/25 10:33: 获取影像接口实现类修改: <br>
 * @author caoxin
 * @return
 * @since 2019/3/25 10:33
 * @date 2019/3/25 10:33
 * @version 0.2
 */


@Service("getImageFileService")
public class GetImageFileServiceImpl implements GetImageFileService{
	/** 日志 */
	private static Logger logger = LoggerFactory.getLogger(GetImageFileServiceImpl.class);
	/** 消息模板 */
	private static final String msgTemplate = "message{ }";

	/**
	 * 获取影像文件接口: <br>
	 *
	 * 0.1:caoxin:2019/3/25 10:23: TODO: <br>
	 * 0.2:caoxin:2019/3/28 修改报文拼装
	 * @param sessionId 为用信流水号
	 * @param reqSn 为请求方需要保证永远唯一响应方据此判断是否重复请求
	 * @param transactionId 事务ID，贯穿 授信、用信
	 * @author caoxin
	 * @return
	 * @since 2019/3/25 10:23
	 * @date 2019/3/25 10:23
	 * @version 0.2
	 */
	@Override
	public RespGetImageFileBean getImageFile(ReqGetImageFileBean reqGetImageFileBean, DXMBasicInfo basicInfo, String sessionId,String transactionId,String reqSn){
		RespGetImageFileBean respGetImageFileBean = null;
		String msg1 = "++++====>获取影像接口调用开始<====++++";
		logger.info(msg1,msgTemplate);
		DXMReqBodyBuilder.Builder requestBean = DXMReqBodyBuilder.ReqBuilder().
				buildAsyncRequest().
				buildRqsFlg(reqSn).
				buildMessageNecessityInfo(sessionId, transactionId).
				buildMessageEvTypeOfQuery().
				buildMessageBasicInfo(basicInfo).builderExpanding(reqGetImageFileBean).build();
		logger.info("++++====>获取影响照片的报文是为<====++++");
		logger.info(JSONObject.toJSONString(requestBean));
		String retCode = "";
		try {
			respGetImageFileBean = ESBClientFactory.
					buildClient().
					withTradeNo(DxmConstant.TRADE_NO_60120004).
					withScene(DxmConstant.SCENE_01).
					withBody(requestBean).
					withESBBeanFactory(LhxESBBeanCreator.getINSTANCE()).
					create().
					sendAndReceive().getBody(RespGetImageFileBean.class);
			
			if (Objects.nonNull(respGetImageFileBean)) {
				// 获取请求结果
				retCode = respGetImageFileBean.getRetCd();
				if (DxmEnum.RetCd.SUCCESS.getValue().equals(retCode)) {
					String msg2 = "++++====>请求获取影像接口调用成功<====++++";
					logger.debug(msg2, msgTemplate);
				} else {
					String msg3 = "++++====>请求获取影像接口调用失败<====++++" + DxmConstant.resultMap.get(retCode);
					logger.error(msg3);
					throw new ESBException(msg3);
				}
			}
		} catch (ESBException e) {
			String msg = "++++====>ESB调用接口出现异常<====++++"+e;
			logger.debug(msg,msgTemplate);
		}
		
		return respGetImageFileBean;
	}

}
